// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
	_before: function() { // 通用预处理器

	},
	//壁纸标签相关函数  ny-wallpaper
	async addWallpaper(itam) {
		const db = uniCloud.database();
		const collection = db.collection("ny-wallpaper");
		let res = await collection.add(itam)
		return {
			code: 200,
			msg: '添加成功',
			data: res
		}
	},
	// 查询含有指定标签的数据 // 升序(asc) 或 降序(desc)
	async lookWallpaper(item, itam) {
		const db = uniCloud.database();
		let currentPage = itam ? itam : 1 //当前页数
		let pageSize = 81 //每页记录数
		const collection = db.collection("ny-wallpaper");
		let total = await collection.where({
			"tags": item
		}).count()
		let totalCounts = total.total //总数据数量
		const totalPage = Math.ceil(totalCounts / pageSize) //计算总共能分多少页
		if (currentPage == totalPage) {
			let res = await collection.where({
					"tags": item
				}).orderBy("create_date", "desc").skip((currentPage - 1) * pageSize)
				.limit(pageSize).get()
			return {
				code: 200,
				msg: '最后分页查询成功',
				data: res.data
			}
		} else {
			let res = await collection.where({
					"tags": item
				}).orderBy("create_date", "desc").skip((currentPage - 1) * pageSize)
				.limit(pageSize).get()
			return {
				code: 200,
				msg: '次级查询成功',
				data: res.data
			}
		}


	},
	// 壁纸下载量
	async wallpaperDown(_id){
		const db = uniCloud.database();
		const collection = db.collection("ny-wallpaper");
		let res = await collection.where({"_id":_id}).get()
		if(res.data[0]){
			let down = res.data[0].down-0+1
			let rea = await collection.doc(_id).update({"down":down})
			return {
				code: 200,
				msg: '修改成功!!',
				data: rea
			}
		}
		
	},
	//获取最新壁纸
	async getWallpaperSt(){
		const db = uniCloud.database();
		const collection = db.collection("ny-wallpaper");
		let res = await collection.orderBy("create_date", "desc").limit(21).get()
		return {
			code: 200,
			msg: '获取成功!!',
			data: res.data
		}
	},
	// 修改壁纸信息
	async updateWallpaper(id,item){
		const db = uniCloud.database();
		const collection = db.collection("ny-wallpaper");
		let res = await collection.doc(id).update(item)
		return {
			code: 200,
			msg: '修改成功!!',
			data: res
		}
	}
}